package com.sgcc.nfc.lock.presentation.locks

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.google.android.material.snackbar.Snackbar
import com.sgcc.nfc.lock.R
import com.sgcc.nfc.lock.databinding.FragmentLockDetailBinding
import com.sgcc.nfc.lock.presentation.nfc.NfcOperationActivity
import com.sgcc.nfc.lock.presentation.nfc.NfcOperationViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class LockDetailFragment : Fragment() {

    private var _binding: FragmentLockDetailBinding? = null
    private val binding get() = _binding!!
    private val viewModel: LockDetailViewModel by viewModels()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentLockDetailBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupListeners()
        observeViewModel()
    }

    override fun onResume() {
        super.onResume()
        viewModel.loadDetail()
    }

    private fun setupListeners() {
        binding.unlockButton.setOnClickListener { launchNfcOperation(NfcOperationViewModel.Mode.OPEN) }
        binding.lockButton.setOnClickListener { launchNfcOperation(NfcOperationViewModel.Mode.CLOSE) }
    }

    private fun observeViewModel() {
        viewModel.lock.observe(viewLifecycleOwner) { lock ->
            if (lock != null) {
                binding.lockName.text = lock.lockName
                
                // 设置锁具图标
                binding.lockIcon.setImageResource(
                    when (lock.status) {
                        com.sgcc.nfc.lock.domain.model.LockStatus.OPEN -> R.drawable.ic_lock_open_24
                        com.sgcc.nfc.lock.domain.model.LockStatus.CLOSED -> R.drawable.ic_lock_24
                        com.sgcc.nfc.lock.domain.model.LockStatus.MALFUNCTION -> R.drawable.ic_warning_24
                        com.sgcc.nfc.lock.domain.model.LockStatus.UNKNOWN -> R.drawable.ic_help_24
                    }
                )
                
                // 设置状态芯片
                binding.lockStatusChip.text = when (lock.status) {
                    com.sgcc.nfc.lock.domain.model.LockStatus.CLOSED -> getString(R.string.lock_status_closed)
                    com.sgcc.nfc.lock.domain.model.LockStatus.OPEN -> getString(R.string.lock_status_open)
                    com.sgcc.nfc.lock.domain.model.LockStatus.MALFUNCTION -> getString(R.string.lock_status_malfunction)
                    com.sgcc.nfc.lock.domain.model.LockStatus.UNKNOWN -> getString(R.string.lock_status_unknown)
                }
                
                // 根据状态设置芯片颜色
                val chipBackgroundColor = when (lock.status) {
                    com.sgcc.nfc.lock.domain.model.LockStatus.CLOSED -> R.color.status_safe
                    com.sgcc.nfc.lock.domain.model.LockStatus.OPEN -> R.color.status_warning
                    com.sgcc.nfc.lock.domain.model.LockStatus.MALFUNCTION -> R.color.status_danger
                    com.sgcc.nfc.lock.domain.model.LockStatus.UNKNOWN -> R.color.status_unknown
                }
                binding.lockStatusChip.setChipBackgroundColorResource(chipBackgroundColor)
                
                binding.lockRoom.text = lock.roomName ?: getString(R.string.unknown_room)
                binding.lockNfcId.text = lock.lockNfcId
                binding.lockLocation.text = if (lock.latitude != null && lock.longitude != null) {
                    getString(R.string.nfc_location_format, lock.latitude, lock.longitude)
                } else {
                    getString(R.string.nfc_location_placeholder)
                }
            }
        }
        viewModel.isLoading.observe(viewLifecycleOwner) { loading ->
            binding.operationProgress.isVisible = loading
        }
        viewModel.message.observe(viewLifecycleOwner) { message ->
            message?.let { Snackbar.make(binding.root, it, Snackbar.LENGTH_LONG).show() }
        }
    }

    private fun launchNfcOperation(mode: NfcOperationViewModel.Mode) {
        val lock = viewModel.lock.value
        if (lock == null) {
            Snackbar.make(binding.root, R.string.common_loading, Snackbar.LENGTH_SHORT).show()
            return
        }
        val intent = NfcOperationActivity.createIntent(
            requireContext(),
            lockNfcId = lock.lockNfcId,
            roomId = lock.roomId,
            mode = mode
        )
        startActivity(intent)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
